//
//  MainViewController.m
//  project
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "MainViewController.h"
#import "ZZYTitleScrollView.h"
#import "OtherTableViewController.h"
#import "SelectViewController.h"
#import "MJRefresh.h"
#import "SearchViewController.h"
@interface MainViewController ()<ZZYTitleScrollViewDelegate,UIScrollViewDelegate>
@property (strong,nonatomic) ZZYTitleScrollView *titleScrollView;
@property (strong,nonatomic) UIScrollView *scrollView;
@property (strong,nonatomic) OtherTableViewController *giftVC;
@property (strong,nonatomic) OtherTableViewController *cateVC;
@property (strong,nonatomic) OtherTableViewController *digitalVC;
@property (strong,nonatomic) OtherTableViewController *movementVC;
@property (strong,nonatomic) OtherTableViewController *entertainmentVC;

@property (strong,nonatomic) SelectViewController *selectVC;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(kWidth / 2 - 20, 30, 40, 20);
    label.text = @"首页";
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    self.navigationItem.title = @"首页";
    self.automaticallyAdjustsScrollViewInsets = NO;
   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"sousuo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(search:)];

    [self initScrollView];
    [self initTitleScrollView];

}
-(void)search:(UIBarButtonItem *)search
{
    SearchViewController *searchVC = [[SearchViewController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}


/*
 在父视图添加titlesdelegate
 */
-(void)initTitleScrollView
{
    NSArray *titleArray = @[@"精选",@"礼物",@"美食",@"数码",@"运动",@"娱乐"];
    _titleScrollView = [[ZZYTitleScrollView alloc]initWithFrame:CGRectMake(0, 60, kWidth, 40)];
    _titleScrollView.buttonWidth = 60;
    _titleScrollView.pagingEnabled = YES;
    [_titleScrollView setupWithTitleArray:titleArray];
    _titleScrollView.backgroundColor = [UIColor whiteColor];
    
    _titleScrollView.titlesdelegate = self;
    [self.view addSubview:_titleScrollView];
}

-(void)initScrollView
{
    NSArray *contentArray = @[@"",@"111",@"118",@"121",@"123",@"120"];

    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, kWidth, kHeight - 100)];
    _scrollView.backgroundColor =[UIColor colorWithRed:0.262 green:0.256 blue:0.196 alpha:1.000];
    //_scrollView.translatesAutoresizingMaskIntoConstraints
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(kWidth*contentArray.count, kHeight - 100);
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    [self.view addSubview:_scrollView];
    _giftVC = [[OtherTableViewController alloc]initWithStyle:UITableViewStylePlain
                                               ];
    _cateVC = [[OtherTableViewController alloc]initWithStyle:UITableViewStylePlain
               ];
    _digitalVC = [[OtherTableViewController alloc]initWithStyle:UITableViewStylePlain
               ];
    _movementVC = [[OtherTableViewController alloc]initWithStyle:UITableViewStylePlain
               ];
    _entertainmentVC = [[OtherTableViewController alloc]initWithStyle:UITableViewStylePlain
               ];
    NSArray *tabArray = @[_giftVC,_giftVC,_cateVC,_digitalVC,_movementVC,_entertainmentVC];
    for (int i = 0; i < contentArray.count; i++) {
        if (i == 0) {
            _selectVC = [[SelectViewController alloc]init];
            _selectVC.navigationC = self.navigationController;
            _selectVC.view.frame = CGRectMake(0, 0, kWidth, kHeight);
            [_scrollView addSubview:_selectVC.view];
        }else{
        ((OtherTableViewController *)tabArray[i]).contentId = contentArray[i];
        ((OtherTableViewController *)tabArray[i]).tableView.frame = CGRectMake(i*kWidth, 0, kWidth, kHeight - 100);
        ((OtherTableViewController *)tabArray[i]).navigationC = self.navigationController;
        ((OtherTableViewController *)tabArray[i]).tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_scrollView addSubview:((OtherTableViewController *)tabArray[i]).tableView];
        }
    }
    
    
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    [_titleScrollView changeViewWithTag:scrollView.contentOffset.x/kWidth ];
}

//回调方法
-(void)scrollviewShouldScollByTitleScollview:(float)Xpercent
{
    
    [self.scrollView setContentOffset:CGPointMake(Xpercent*kWidth, 0) animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
